package ajaxtest;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.umgsai.utils.DateUtil;

public class qiangdaAction {

	public void start_qinagda() {
		DBManager dbManager = new DBManager();
		// 设置之前所有的抢答结束
		String sql = "update qiangda_group set is_end = 1 where 1=1";
		System.out.println(dbManager.executeUpdate(sql));
		sql = "insert into qiangda_group (create_time,started,is_end)values('"
				+ DateUtil.GetTime() + "',1,0)";
		//
		System.out.println(sql);
		dbManager.insertSQL(sql);
		// System.out.println(dbManager.insertSQL(sql));
		// long a = Calendar.getInstance().getTimeInMillis();
		// System.err.println(a);

		try {
			String s = "{\"name\":\"提示\",\"age\":\"新一轮抢答开始！！！\"}";

			ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
			ServletActionContext.getResponse().getWriter().print(s);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbManager.closeConnection();

		}

	}

	public void qiangda() {
		DBManager dbManager = new DBManager();
		String sqlString = "select * from qiangda_group where is_end =0";
		System.err.println(sqlString);
		String ip = ServletActionContext.getRequest().getParameter("ip");
		System.out.println(ip);
		ResultSet resultSet = dbManager.executeQuery(sqlString);
		qiangda_group qiangda_group = null;
		try {
			if (resultSet.next()) {
				qiangda_group = new qiangda_group();
				qiangda_group.setCreate_time(resultSet.getDate("create_time"));
				qiangda_group.setGroup_id(resultSet.getInt("group_id"));
				qiangda_group.setIs_end(resultSet.getInt("is_end"));
				qiangda_group.setStarted(resultSet.getInt("started"));
				System.out.println(qiangda_group);
				// 插入新纪录之前要查询是否已经抢答过了
				String sql = "select * from qiangda where ip = '" + ip
						+ "' and group_id =" + qiangda_group.getGroup_id();
				resultSet = dbManager.executeQuery(sql);
				if (resultSet.next()) {
					System.out.println("本轮已经抢答过了！！！");
					String s = "{\"name\":\"提示\",\"age\":\"本轮已经抢答过了！！！\"}";
					ServletActionContext.getResponse().setCharacterEncoding(
							"UTF-8");
					ServletActionContext.getResponse().getWriter().print(s);
				} else {
					sql = "insert into qiangda (group_id,ip,qiangda_time)values ("
							+ qiangda_group.getGroup_id()
							+ ",'"
							+ ip
							+ "','"
							+ String.valueOf(Calendar.getInstance()
									.getTimeInMillis()) + "')";
					System.err.println(sql);
					dbManager.insertSQL(sql);
					System.out.println("抢答成功！！！");
					String s = "{\"name\":\"提示\",\"age\":\"已提交抢答申请，请静候抢答结果！！！\"}";
					ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
					ServletActionContext.getResponse().getWriter().print(s);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			dbManager.closeConnection();

		}

	}

	public void view_latest_result() {
		DBManager dbManager = new DBManager();
		String sqlString = "select max(group_id) as max_id from qiangda_group ";
		try {
			System.out.println(sqlString);
			ResultSet resultSet = dbManager.executeQuery(sqlString);
			if (resultSet.next()) {
				int max_id = resultSet.getInt("max_id");
				sqlString = "select * from qiangda where group_id = "+max_id+" order by qiangda_time";
				System.out.println(sqlString);
				resultSet = dbManager.executeQuery(sqlString);
				List<qiangda> qiangdaList = new ArrayList<qiangda>();
				qiangda qiangda = null;
				while (resultSet.next()) {
					qiangda = new qiangda();
					qiangda.setGroup_id(resultSet.getInt("group_id"));
					qiangda.setIp(resultSet.getString("ip"));
					qiangda.setQiangda_id(resultSet.getInt("qiangda_id"));
					qiangda.setQiangda_time( Long.parseLong(resultSet.getString("qiangda_time")));
					qiangdaList.add(qiangda);
					
				}
				JSONArray jsonArray = JSONArray.fromObject(qiangdaList);  
				
				ActionContext.getContext().getSession().put("qiangdaList", qiangdaList);
				System.out.println(jsonArray);
				//String s = "{\"name\":\"提示\",\"age\":\"已提交抢答申请，请静候抢答结果！！！\"}";
				ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
				ServletActionContext.getResponse().getWriter().print(jsonArray);
				/*
				 [
				 {"qiangda_id":15,"group_id":56,"qiangda_time":1386253774144,"ip":"0:0:0:0:0:0:0:1"}
				 ,
				 {"qiangda_id":16,"group_id":56,"qiangda_time":1386253977924,"ip":"192.168.43.176"}
				 ,
				 {"qiangda_id":17,"group_id":56,"qiangda_time":1386253988012,"ip":"192.168.24.70"}
				 ]
				 * 
				 * {\"name\":\"提示\",\"age\":\"已提交抢答申请，请静候抢答结果！！！\"}
				 * */
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			dbManager.closeConnection();

		}

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		qiangda qiangda = new qiangda ();
		qiangda.setGroup_id(1);
		qiangda.setIp("127.0.0.1");
		qiangda.setQiangda_id(12);
		qiangda.setQiangda_time((Long.getLong("1245792323546")));
		JSONArray jsonArray = JSONArray.fromObject(qiangda); 
		System.out.println(jsonArray);
	}

}
